package com.change.service.system;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.change.core.page.Page;
import com.change.dto.req.QuerySystemMenuReq;
import com.change.dto.req.SaveSystemMenuReq;
import com.change.dto.resp.QuerySystemMenuResp;
import com.change.entity.system.SystemMenu;
import com.change.exception.BizException;
import com.change.exception.ResultStatus;
import com.change.mapper.system.SystemMenuMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @author jhlz
 * @date 2022-08-27
 */
@Slf4j
@Service
public class SystemMenuService extends ServiceImpl<SystemMenuMapper, SystemMenu> {
    private final SystemMenuMapper sysMenuMapper;

    public SystemMenuService(SystemMenuMapper sysMenuMapper) {
        this.sysMenuMapper = sysMenuMapper;
    }

    public Page<List<QuerySystemMenuResp>> getPage(Page page, QuerySystemMenuReq req) {

        return page;
    }

    public QuerySystemMenuResp detail(Integer id) {
        SystemMenu sysMenu = this.sysMenuMapper.selectById(id);
        SystemMenu menuDetail = Optional.ofNullable(sysMenu)
                .orElseThrow(() -> new BizException(ResultStatus.Common.NOT_FOUND));
        QuerySystemMenuResp resp = new QuerySystemMenuResp();
        BeanUtil.copyProperties(menuDetail, resp);
        return resp;
    }

    @Transactional(rollbackFor = Exception.class)
    public SystemMenu save(SaveSystemMenuReq req) {
        SystemMenu menu = new SystemMenu();
        BeanUtil.copyProperties(req, menu);
        return menu;
    }

    @Transactional(rollbackFor = Exception.class)
    public SystemMenu update(SaveSystemMenuReq req) {
        SystemMenu menu = sysMenuMapper.selectById(req.getId());
        BeanUtil.copyProperties(req, menu);
        this.sysMenuMapper.updateById(menu);
        return menu;
    }

    @Transactional(rollbackFor = Exception.class)
    public Integer delete(Integer id) {
        return this.sysMenuMapper.deleteById(id);
    }

    public List<SystemMenu> getMenuList() {
        return this.sysMenuMapper.getMenuList();
    }
}
